<?php

App::uses('TestEnvironment', 'Lib');
App::uses('FileSystem', 'Base.Lib');
App::uses('ConfigurationKeys', 'ConfigurationKeys.Lib');
App::uses('PsfmAdmin', 'Lib');

class RawFileTest extends CakeTestCase {

    public $fixtures = array(
        'plugin.ConfigurationKeys.SettedConfigurationKey',
        'RawFile',
        'RawFileTag',
        'SourceDocument',
        'SourceDocumentTag',
        'Tag',
    );

    /**
     *
     * @var RawFile
     */
    private $RawFile;

    public function setUp() {
        parent::setUp();

        ConfigurationKeys::setKeyValue(
            'raw_files_directory'
            , FileSystem::createTemporaryDirectory()
        );

        ConfigurationKeys::setKeyValue(
                'shared_files_directory'
                , FileSystem::createTemporaryDirectory()
        );

        ConfigurationKeys::setKeyValue(
                'joined_document_regex'
                , '/^([\S]+)/'
        );

        PsfmAdmin::customDataModelsClearCache();
        $this->RawFile = ClassRegistry::init('RawFile');
        $this->RawFileTag = ClassRegistry::init('RawFileTag');
        $this->Tag = ClassRegistry::init('Tag');
        $this->SourceDocument = ClassRegistry::init('SourceDocument');
    }

    public function tearDown() {
        FileSystem::recursiveRemoveDirectory(
                ConfigurationKeys::getKeyValue('raw_files_directory')
        );
        parent::tearDown();
    }

    public function testCreate() {
        $fileName = 'raw_file_test';
        $this->assertEqual(file_exists(ConfigurationKeys::getKeyValue('raw_files_directory') . DS . $fileName), false);
        $this->RawFile->create();
        $result = $this->RawFile->save(array(
            $this->RawFile->alias => array(
                'name' => $fileName
            )
            ));
        
        $this->assertEqual($this->RawFile->validationErrors, array());
        $this->assertNotEqual($result, false);

        $this->assertEqual(file_exists(ConfigurationKeys::getKeyValue('raw_files_directory') . DS . $fileName), true);
        $rawFile = $this->RawFile->findByName('raw_file_test');
        $this->assertEqual(empty($rawFile), false);
        $this->assertEqual(empty($rawFile['RawFile']['source_document_id']), false);
    }

    public function testFindByLike() {
        for ($i = 1; $i <= 20; $i++) {
            $this->RawFile->create();
            $result = $this->RawFile->save(array(
                $this->RawFile->alias => array(
                    'name' => $i . '_raw_file'
                )
                ));

            $this->assertEqual($this->RawFile->validationErrors, array());
            $this->assertNotEqual($result, false);
        }

        $this->assertEquals(count($this->RawFile->find(
                    'all', array(
                    'conditions' => array(
                        'RawFile.name like' => '2%'
                    )
                    )
                )), 2);

        $this->assertEquals(count($this->RawFile->find(
                    'all', array(
                    'conditions' => array(
                        'RawFile.name like ?' => '2%'
                    )
                    )
                )), 2);
    }

    public function testRename() {
        //Check no rows saved
        $this->assertEqual($this->RawFile->find('count'), 0);
        $this->assertEqual($this->RawFileTag->find('count'), 0);

        //Create raw file
        $this->assertEqual(file_exists(ConfigurationKeys::getKeyValue('raw_files_directory') . DS . 'document file 1'), false);
        $this->RawFile->create();
        $result = $this->RawFile->save(array(
            $this->RawFile->alias => array(
                'name' => 'document file 1'
            )
        ));
        $this->assertEqual($this->RawFile->validationErrors, array());
        $this->assertNotEqual($result, false);
        $this->assertEqual(file_exists(ConfigurationKeys::getKeyValue('raw_files_directory') . DS . 'document file 1'), true);
        $this->assertEqual($this->RawFile->find('count'), 1);
        $this->assertEqual($this->SourceDocument->find('count'), 1);
        $this->assertEqual($this->RawFileTag->find('count'), 0);
        $rawFile = $this->RawFile->find('first');
        $sourceDocument = $this->SourceDocument->find('first');
        $this->assertEqual($sourceDocument['SourceDocument']['name'], 'document');

        //Create raw file tag
        $this->Tag->create();
        $this->Tag->save(array(
            'Tag' => array(
                'name' => 'Tag test'
            )
        ));
        $tag = $this->Tag->read();
        $this->assertEqual(empty($tag), false);

        $rawFileTag['RawFileTag'] = array(
            'raw_file_id' => $rawFile['RawFile']['id'],
            'tag_id' => $tag['Tag']['id'],
            'name' => $rawFile['RawFile']['name']
        );
        $this->RawFileTag->create();
        $this->RawFileTag->save($rawFileTag);
        $rawFileTag = $this->RawFileTag->read();
        $this->assertEqual(empty($rawFileTag), false);

        $this->assertEqual($this->RawFile->find('count'), 1);
        $this->assertEqual($this->RawFileTag->find('count'), 1);
        $this->assertEqual($this->SourceDocument->find('count'), 1);

        //Rename raw fle
        $this->assertEqual($rawFile['RawFile']['name'], 'document file 1');
        $rawFile['RawFile']['name'] = 'changed file 1';
        $result = $this->RawFile->save($rawFile);
        $this->assertEqual($this->RawFile->validationErrors, array());
        $this->assertNotEqual($result, false);

        $this->assertEqual($this->RawFile->find('count'), 1);
        $this->assertEqual($this->RawFileTag->find('count'), 1);
        $this->assertEqual($this->SourceDocument->find('count'), 1);

        $rawFile = $this->RawFile->find('first');
        $this->assertEqual($rawFile['RawFile']['name'], 'changed file 1');
        $this->assertEqual(file_exists($this->RawFile->getAbsolutePath($rawFile)), true);
        $rawFileTag = $this->RawFileTag->find('first');
        $this->assertEqual($rawFileTag['RawFileTag']['name'], 'changed file 1');
        $this->assertEqual(file_exists($this->RawFileTag->absolutePath($rawFileTag)), true);
        $sourceDocument = $this->SourceDocument->find('first');
        $this->assertEqual($sourceDocument['SourceDocument']['name'], 'changed');
    }

}